package com.pbdevj.jdbc.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil2
{
	public static Connection getConn()
	{
		Connection conn = null;

		InputStream is = Object.class.getResourceAsStream("/map/mysql.properties");

		Properties prop = new Properties();

		// 1.加载驱动类
		try
		{
			prop.load(is);

			String driverName = prop.getProperty("mysql.driver");

			String url = prop.getProperty("mysql.url");

			String userName = prop.getProperty("mysql.username");

			String password = prop.getProperty("mysql.password");

			Class.forName(driverName);

			// 2.获取连接
			conn = DriverManager.getConnection(url, userName, password);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return conn;
	}

	public static void closeConn(ResultSet rs, PreparedStatement ps, Connection conn)
	{
		if (rs != null)
		{
			try
			{
				rs.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}

		if (ps != null)
		{
			try
			{
				ps.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}

		if (conn != null)
		{
			try
			{
				conn.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
	}
}
